Imports Microsoft.VisualBasic
Imports System.Data.SqlClient

Namespace MyDAO
    Public MustInherit Class MyDataRelationBase
        Public Table As MyDataTableBase
        Public Condition As MyDataConditionBase

        Public Cmd As SqlCommand

        Public Sub New()
            Me.Cmd = New SqlCommand
            Me.Cmd.CommandType = Data.CommandType.Text
        End Sub

        Public Sub AddCondition(ByVal columnName As String, ByVal value As Object)
            Me.AddCondition(columnName, MyEqualityOperator.Equal, value)
        End Sub

        Public Sub AddCondition(ByVal columnName As String, ByVal op As MyEqualityOperator, ByVal value As Object)
            Me.AddCondition(MyLogicalOperator.MyAnd, CreateCondition(New MyColumnExpression(columnName), op, New MyValueExpression(value)))
        End Sub

        Public Sub AddVarCondition(ByVal columnName As String, ByVal variable As String)
            Me.AddVarCondition(columnName, MyEqualityOperator.Equal, variable)
        End Sub

        Public Sub AddVarCondition(ByVal columnName As String, ByVal op As MyEqualityOperator, ByVal variable As String)
            Me.AddCondition(MyLogicalOperator.MyAnd, CreateCondition(New MyColumnExpression(columnName), op, New MyVariableExpression(variable)))
        End Sub

        Public Sub AddCondition(ByVal op As MyLogicalOperator, ByVal condition As MyDataConditionBase)
            If Me.Condition Is Nothing Then
                Me.Condition = condition
            Else
                Dim condition1 As MyDataConditionBase = Me.Condition
                Me.Condition = New MyCompositeCondition(condition1, op, condition)
            End If

        End Sub

        Public Sub OrCondition(ByVal columnName As String, ByVal value As Object)
            Me.OrCondition(columnName, MyEqualityOperator.Equal, value)
        End Sub

        Public Sub OrCondition(ByVal columnName As String, ByVal op As MyEqualityOperator, ByVal value As Object)
            Me.AddCondition(MyLogicalOperator.MyOr, CreateCondition(New MyColumnExpression(columnName), op, New MyValueExpression(value)))
        End Sub

        Public Sub OrVarCondition(ByVal columnName As String, ByVal variable As String)
            Me.OrVarCondition(columnName, MyEqualityOperator.Equal, variable)
        End Sub

        Public Sub OrVarCondition(ByVal columnName As String, ByVal op As MyEqualityOperator, ByVal variable As String)
            Me.AddCondition(MyLogicalOperator.MyOr, CreateCondition(New MyColumnExpression(columnName), op, New MyVariableExpression(variable)))
        End Sub

        Public Shared Function CreateCondition(ByVal exp1 As MyDataExpressionBase, ByVal op As MyEqualityOperator, ByVal exp2 As MyDataExpressionBase)
            Return New MyEqualityCondition(exp1, op, exp2)
        End Function

        Public Sub AddWithValue(ByVal paremeterName As String, ByVal value As Object)
            Me.Cmd.Parameters.AddWithValue(paremeterName, value)
        End Sub

    End Class
End Namespace
